package com.king.configuration.jpa;

import com.king.pojo.entity.SysUser;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Optional;

/**
 * @author liukai
 */
public class SpringSecurityAuditorAware implements AuditorAware<SysUser> {

    @Override
    public Optional<SysUser> getCurrentAuditor() {
        return Optional.ofNullable(SecurityContextHolder.getContext())
                .map(SecurityContext::getAuthentication)
                .filter(Authentication::isAuthenticated)
                .map(Authentication::getPrincipal)
                .map(SysUser.class::cast);
    }

}
